package com.beauty.dressing.module.common.view;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.framework.common.base.IBaseDialog;
import com.framework.common.utils.ICommonUtil;
import com.framework.common.utils.IDisplayUtil;
import com.framework.common.utils.IKeyboardUtils;
import com.beauty.dressing.common.utils.ColorTextUtil;
import com.beauty.dressing.common.utils.ResUtil;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.rxandroid.rxbus.RxAction;
import com.beauty.dressing.rxandroid.rxbus.RxBus;
import com.beauty.dressing.rxandroid.rxbus.RxBusObserver;
import com.beauty.dressing.rxandroid.rxbus.RxConstants;
import com.beauty.dressing.R;

import java.util.Collections;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/04/04
 */

public class InputEditDialog extends IBaseDialog implements View.OnClickListener{
    private EditText mInput;
    private Button mSureBtn;
    private OnEditListener listener;

    public InputEditDialog(Context context,OnEditListener listener) {
        super(context, R.style.TransparentDialog);
        this.listener = listener;
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.dialog_input_edit);
    }

    @Override
    public void findView() {
        mInput = (EditText) findViewById(R.id.input);
        mSureBtn = (Button) findViewById(R.id.sure_btn);
        mSureBtn.setOnClickListener(this);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.DialogPopupAnimation);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = IDisplayUtil.getScreenWidth(mContext);
        window.setAttributes(lp);
    }

    @Override
    public void initData() {
        mInput.requestFocus();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void show() {
        super.show();
        LBController.MainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                IKeyboardUtils.openKeybord(mContext,mInput);
            }
        }, 300);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        LBController.MainHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                IKeyboardUtils.closeKeybord(mContext);
            }
        }, 300);

    }

    @Override
    public void onClick(View v) {
        if (ICommonUtil.isFastDoubleClick()) return;
        switch (v.getId()){
            case R.id.sure_btn:
                String inputStr = mInput.getText().toString();
                if (inputStr.length() == 0){
                    showToast("请输入正确的数值");
                    return;
                }
                int num = Integer.parseInt(inputStr);
                if (num ==0 || num >= 1000){
                    showToast("数值在0-1000之间");
                    return;
                }
                if (listener != null){
                    listener.editResult(inputStr,num);
                }
                break;
        }
    }

    public interface OnEditListener{
        void editResult(String result,int num);
    }

}
